package no.hib.bridge.gui;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
//import javax.swing.JPopupMenu;
import javax.swing.UIManager;

//import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import no.hib.bridge.net.BridgeHttpServer;

import javax.swing.JList;

import net.miginfocom.swing.MigLayout;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

/**
 * Main GUI for the Spar program
 * 
 * @author Henning and Georg
 * 
 */
public class SparMainGUI {

	private JFrame frame;
	private JTextField textField;
	private File DBFile;
	public String DBLocation;
	JList list;
	static DefaultListModel dlm;
	private BridgeHttpServer server;
	JButton btnStartServer;
	JButton btnStopServer;
	private JButton btnSupedoClient;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					SparMainGUI window = new SparMainGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SparMainGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * 
	 * @param DBLocation
	 */
	private void initialize() {
		// try {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
		}


		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.getContentPane().setLayout(
				new MigLayout("", "[grow][217px][10px][77px][10px][100px]",
						"[][][][][34px][134px,grow][23px][][]"));

		JLabel lblDatabaseFileLocation = new JLabel("Database file location:");
		frame.getContentPane().add(lblDatabaseFileLocation, "cell 0 0");

		textField = new JTextField();
		textField.addInputMethodListener(new InputMethodListener() {
			@Override
			public void inputMethodTextChanged(InputMethodEvent arg0) {
				DBLocation = textField.getText();
			}

			@Override
			public void caretPositionChanged(InputMethodEvent event) {
				// TODO Auto-generated method stub

			}
		});
		textField.setColumns(10);
		frame.getContentPane().add(textField,
				"cell 0 1 4 1,growx,aligny bottom");

		final JButton btnBrowse = new JButton("Browse");

		btnBrowse.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				/*
				 * New file has been selected
				 */
				JFileChooser fc = new JFileChooser(DBLocation);
				fc.showOpenDialog(btnBrowse);
				DBFile = fc.getSelectedFile();
				DBLocation = DBFile.getPath();
				FileChanged();
			}
		});
		frame.getContentPane().add(btnBrowse, "cell 5 1,growx,aligny bottom");


		btnStartServer = new JButton("Start Server");
		btnStartServer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try{
					if (server == null) {
						server = new BridgeHttpServer(SparMainGUI.this);
					}

				}catch(Exception e){
					addMessage("web: "+e.getMessage());
				}
				
				try {
					server.start();
					addMessage("Server running");
				} catch (Exception e) {
					addMessage("Server start failed: "+e.getLocalizedMessage());
				}
				
				btnStartServer.setEnabled(false);
				btnStopServer.setEnabled(true);
			}
		});
		frame.getContentPane().add(btnStartServer, "cell 0 7,growx");
		btnStopServer = new JButton("Stop Server");
		btnStopServer.setEnabled(false);
		btnStopServer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					server.stop();
					server=null;
					btnStartServer.setEnabled(true);
					btnStopServer.setEnabled(false);
					SparMainGUI.this.addMessage("Server Stopped");
				} catch (Exception e) {
					e.printStackTrace();
					addMessage("Server Stop failed "+e.getLocalizedMessage());
				}
			}
		});
		frame.getContentPane().add(btnStopServer, "cell 0 8,grow");

		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exitProgram();
			}
		});
		
		btnSupedoClient = new JButton("Add server link to Supedo");
		btnSupedoClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SupedoClientGUI clientGUI = new SupedoClientGUI();
				clientGUI.setVisible(true);
			}
		});
		frame.getContentPane().add(btnSupedoClient, "cell 1 8,grow");
		frame.getContentPane().add(btnExit, "cell 5 8,growx");

		dlm = new DefaultListModel();
		list = new JList(dlm);
		frame.getContentPane().add(list, "flowx,cell 0 5 6 1,grow");
		list.setBounds(10, 69, 414, 134);
	}

	// private static void addPopup(Component component, final JPopupMenu popup)
	// {
	// }

	/**
	 * FileChanged() is used, when the file browser is exiting, to update the
	 * textfield
	 */
	private void FileChanged() {
		textField.setText(DBLocation);
	}

	/**
	 * This method is used to add a massage to the list to show when something
	 * has happened
	 * 
	 * @param m
	 *            is the message to insert into the list
	 */
	public void addMessage(String m) {
		dlm.addElement(m);
		//list.setListData(messages);
		//list.repaint();
	}
	
	public void exitProgram(){
		frame.dispose();
		System.exit(0);
	}
}
